<template>
	<view>
		<scroll-view class="scroll" v-if="hasProduct" scroll-y="true" @scrolltolower="lower">
			<shopping-cart-product-list :shopping-cart-product-list="shoppingCartProductList"></shopping-cart-product-list>
		</scroll-view>
		<view v-if="noProduct">
			<shopping-cart-no-product></shopping-cart-no-product>
		</view>
	</view>

</template>

<script>
	import shoppingCartProductList from '@/components/shoppingCartProductList/shoppingCartProductList.vue'
	import shoppingCartNoProduct from '@/components/shoppingCartNoProduct/shoppingCartNoProduct.vue'
	export default {
		components: {
			shoppingCartProductList,
			shoppingCartNoProduct
		},
		data() {
			return {
				shoppingCartProductList: [],
				hasProduct: false,
				noProduct: false,
				page: 1,
				flag: true,
				moreProduct: true
			};
		},
		onShow() {
			this.moreProduct = true;
			let self = this;
			uni.showLoading({
			    title: '加载中'
			});
			uni.request({
				url: this.$store.state.URL+"/goods/shopping_cart/shopping_cart",
				data: {
					token: this.$store.state.token,
				},
				success(res) {
					console.log(res.data);
					self.shoppingCartProductList = [];
					let arr = res.data.data.data;
					if(arr.length > 0){
						for(let i=0; i<arr.length; i++){
							let item = {}
							item.productId = arr[i].good_data.id;
							item.productName = arr[i].good_data.goodName;
							item.productImg = arr[i].good_data.mainImg;
							item.productPrice = arr[i].good_data.sellingPrice;
							item.orderNum = arr[i].num;
							item.invalid = arr[i].good_data.isDelete;
							item.checked = false;
							item.isSlide = false;
							item.canClick = true;
							self.shoppingCartProductList.push(item);
						}
						console.log(self.shoppingCartProductList);
						self.hasProduct = true;
						self.noProduct = false;
					}else{
						self.hasProduct = false;
                        self.noProduct = true;
					}
					uni.hideLoading();
				},
				fail(e){
					console.log(e);
				}
			});
		},
		methods: {
			upper(e){
				console.log(e);
			},
			lower(e){
				if(this.flag && this.moreProduct){
					let self = this;
					this.flag = false;
					this.page++;
				    uni.showLoading({
					    title: '加载中'
					});
					uni.request({
						url: this.$store.state.URL+"goods/shopping_cart/shopping_cart",
						data: {
							token: this.$store.state.token,
							page: this.page,
							pagesize: 8,
							merchantID:this.$store.state.merchantDefaultID
						},
						success(res) {
							console.log(res.data);
							uni.hideLoading();
							let arr = res.data.data;
							if(arr.length > 0){
								for(let i=0; i<arr.length; i++){
									let item = {}
									item.productId = arr[i].good_data.id;
									item.productName = arr[i].good_data.goodName;
									item.productImg = arr[i].good_data.mainImg;
									item.productPrice = arr[i].good_data.sellingPrice;
									item.orderNum = arr[i].num;
									item.invalid = arr[i].good_data.isDelete;
									item.checked = false;
									item.isSlide = false;
									item.canClick = true;
									self.shoppingCartProductList.push(item);
								}
								console.log(self.shoppingCartProductList);
							}else{
								self.moreProduct = false;
								uni.showToast({
									title: '没有了',
									icon: 'none',
									duration: 2000
								});
							}
							self.flag = true;
						},
						fail(e){
							console.log(e);
						}
					});
				}else{
					return;
				}
			},
			scroll(e){
				console.log(e);
			}
		}
	}
</script>

<style>
  page{
  	background-color: #F5F5F5;
  }
  .scroll{
	  height: 100vh;
  }
</style>
